package com.whfc.common.util;

import com.whfc.common.generator.KeyGeneratorUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

/**
 * @author sun_guodong
 * @description
 * @date 2020-06-05
 */
public class FileUtil {
    private static final Logger logger = LoggerFactory.getLogger(FileUtil.class);

    /**
     * 拷贝模板文件
     *
     * @return
     * @throws Exception
     */
    public static File copyTemplateFile(InputStream templateFileInputStream) {
        try {
            // 拷贝临时文件
            String fileName = KeyGeneratorUtil.genStrId();
            File tempFile = File.createTempFile(fileName, ".xls");
            tempFile.deleteOnExit();

            FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
            // 将模板复制到临时文件中
            byte[] b = new byte[1024];
            int len = 0;
            while (-1 != (len = templateFileInputStream.read(b))) {
                fileOutputStream.write(b, 0, len);
            }
            fileOutputStream.flush();
            fileOutputStream.close();
            templateFileInputStream.close();
            return tempFile;
        } catch (Exception e) {
            logger.error("复制模板失败:{}", e.getMessage());
        }

        return null;

    }

    /**
     * 读取流数据
     *
     * @param inputStream
     * @return
     */
    public static byte[] readData(InputStream inputStream) {
        try {
            // 拷贝临时文件
            String fileName = KeyGeneratorUtil.genStrId();
            File tempFile = File.createTempFile(fileName, ".xls");
            tempFile.deleteOnExit();

            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            // 将模板复制到临时文件中
            byte[] b = new byte[1024];
            int len = 0;
            while (-1 != (len = inputStream.read(b))) {
                outputStream.write(b, 0, len);
            }
            outputStream.flush();
            outputStream.close();
            inputStream.close();
            return outputStream.toByteArray();
        } catch (Exception e) {
            logger.error("复制模板失败:{}", e.getMessage());
        }
        return null;
    }

    /**
     * 获取文件命名后缀(带点号)
     *
     * @param fileName
     * @return
     */
    public static String getSuffix(String fileName) {
        String suffix = null;
        if (StringUtils.isNotBlank(fileName)) {
            suffix = fileName.substring(fileName.lastIndexOf("."));
        }
        return suffix;
    }

    /**
     * 获取文件命名后缀(不带点号)
     *
     * @param fileName
     * @return
     */
    public static String getSuffixWithoutDot(String fileName) {
        String suffix = null;
        if (StringUtils.isNotBlank(fileName)) {
            suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        }
        return suffix;
    }

    public static void main(String[] args) {
        System.out.println(FileUtil.getSuffix("x.png"));
        System.out.println(FileUtil.getSuffixWithoutDot("x.png"));
    }
}
